﻿using System;
using DirectCanvas;
using DirectCanvas.Effects;
using DirectCanvas.Imaging;
using DirectCanvas.Misc;
using DirectCanvas.Scenes;

namespace WindowsFormsTest.Demos.EffectsDemo
{
    class TransitionEffectScene : Scene
    {
        private readonly Presenter m_presenter;
        private const int SCENE_WIDTH = 1280;
        private const int SCENE_HEIGHT = 720;

        private DrawingLayer m_bitmap;
        private DrawingLayer m_tempLayer;

        private bool m_mousedown;
        private PointF m_mousePosition;
        private RadialWiggleTransitionEffect m_transition;
        private DrawingLayer m_cloudsBitmap;
    
        public TransitionEffectScene(Presenter presenter)
        {
            m_presenter = presenter;
            InitializeResources();
        }

        public override void SetInputState(int inputId, InputStatus status, PointF position)
        {
            m_mousedown = status == InputStatus.Down;
            m_mousePosition = position;
        }

        private float anim = 0;
        private float animStep = 0.008f;
        private DrawingLayer m_mountainsBitmap;


        public override void Render()
        {
            anim += animStep;

            if (anim >= 1 || anim < 0)
                animStep *= -1;

            m_tempLayer.BeginDraw();
            m_tempLayer.Clear();
            m_tempLayer.DrawLayer(m_bitmap, new RectangleF(0, 0, SCENE_WIDTH, SCENE_HEIGHT), 1f);
            m_tempLayer.EndDraw();


            m_transition.Progress = anim;
            m_transition.CloudInput = m_cloudsBitmap;
            m_transition.OldInput = m_mountainsBitmap;
            m_transition.RandomSeed = 0.1f;

            m_tempLayer.ApplyEffect(m_transition, m_presenter);
        }

        private void InitializeResources()
        {
            m_tempLayer = m_presenter.Factory.CreateDrawingLayer(SCENE_WIDTH, SCENE_HEIGHT);
            m_bitmap = m_presenter.Factory.CreateDrawingLayerFromFile(@".\Assets\Radioactive.jpg");

            m_cloudsBitmap = m_presenter.Factory.CreateDrawingLayerFromFile(@".\Assets\clouds.png");
            m_mountainsBitmap = m_presenter.Factory.CreateDrawingLayerFromFile(@".\Assets\Nature Mountains photo.jpg");


            m_transition = new RadialWiggleTransitionEffect(m_presenter.Factory);
        }
    }
}
